package org.NooLab.nativebrowser.retrieval;

import java.io.IOException;
import java.util.Observable;

import org.NooLab.nativebrowser.app.controller.parser.PageIntf;
import org.NooLab.nativebrowser.browser.BrowserWorker;
import org.NooLab.nativebrowser.browser.DjBrowserHost;
import org.NooLab.nativebrowser.browser.DjBrowserListener;
import org.NooLab.nativebrowser.components.WebBrowserAppMainIntf;
import org.NooLab.nativebrowser.components.WebBrowserPublicIntf;
import org.NooLab.nativebrowser.gui.hidden.HiddenWindowGuiIntf;
import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.objects.StringedObjects;

import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;








public class RetrievalDispatch extends Observable{

	WebBrowserAppMainIntf app;
	DjBrowserHost djBrowserHost; 
	DjBrowserListener listener;
	
	PrintLog out = new PrintLog(2,true);
	private BrowserWorker swingWorker;
	private JWebBrowser webBrowser;
	private HiddenWindowGuiIntf windowGui;
	private boolean completedDataProcessing;
	
	DFutils fileutil = new DFutils();
	private WebBrowserPublicIntf resultsClient;
	
	// ========================================================================
	public RetrievalDispatch( WebBrowserAppMainIntf app, 
							  DjBrowserHost djBrowserHost, 
							  DjBrowserListener listener) {
		// 
		this.app = app;
		this.djBrowserHost = djBrowserHost;
		this.listener = listener;
		
		windowGui = app.getWindowGui();
		webBrowser = djBrowserHost.getWebBrowser();
		
		resultsClient = app.getAppCommons().getExposureClient();
		addObserver(resultsClient);
	}

	// ========================================================================
	
	public void close(){
		((Observable)this ).deleteObservers();

	}
	
	public void handlingData(Object pageStrObj) throws IOException {

		PageIntf page=null;
		String url="" ;
		int s1,s2;
		
		if (completedDataProcessing ){
			return;
		}
		completedDataProcessing = true;
		
		listener.stopCompletionDetector();
		
		updateReferences();
		
		
		
		if (out!=null){
			out.print(3, "            LOADING  C O M P L E T E D ! ! ");
			out.print(2, "browser completed loading.");
			out.delay(10);
		}else{
			// System.out.println( "\n\nLOADING  C O M P L E T E D ! ! ");
		}
		StringedObjects so = new StringedObjects();
		
		
		int p ;
		if (pageStrObj instanceof PageIntf){
			url = ((PageIntf)pageStrObj).getContextUrl() ;
			page = ((PageIntf)pageStrObj);
			if (url==null)url="";
			
			
			if (page.getSecret()!=null){
				out.printErr(2, "\n"+">>>>>   secret of page : "+page.getSecret() );
			}
			
			
		}
		
		//System.out.println("\nreceived string : \n"+ (String)pageStrObj ) ;
		
		// now we expose the page to the caller, or to the TCP ...
		// -> resultsClient
		setChanged();
		notifyObservers(page);
		
		out.delay(5) ;
		close();
	}
	
	
	private void updateReferences(){
		windowGui = app.getAppCommons().getWindowGui();
		out = windowGui.getOut();
		// htmlConsumer = app.getAppCommons().getHtmlConsumer();

	}
	

	
	

}
